import { Form, Input, Button, Space,message ,InputNumber} from 'antd';
import shoppingAPI from "../../api/shopping"

import { useHistory ,useParams} from "react-router-dom";
import { useEffect } from "react"


// eslint-disable-next-line
  export default () => {
    let history = useHistory();
    let {_id} =useParams();
    const [form] = Form.useForm();
    
 
     
    async function getinfo(_id) {
      let {num,shoppingid,userid}=await shoppingAPI.find(_id)


      form.setFieldsValue({
        num:num,
        shoppingid:shoppingid,
        userid:userid
      })

    }

    useEffect(() => {
     getinfo(_id);
    }, [_id])



    const onFinish =async values => {
         values._id=_id;
         values.total=values.num*values.price;
        let data=await shoppingAPI.change(values)
        if(data){
          message.success({ 
            content:"修改成功！",
            duration:1,
            onClose:()=>{
              history.push("/info/carts")
            }
          });
        } 
    }; 



    
    
    return  <Form name="complex-form" style={{marginTop:"100px"}} form={form} onFinish={onFinish} labelCol={{ span: 8 }} wrapperCol={{ span: 16 }}>


  
    
    <Form.Item label="商品数量">
      <Space>
        <Form.Item
          name="num"
          noStyle
          rules={[{ required: true, message: '商品数量' }]}
        >
          <InputNumber min={1} style={{ width: 160 }} placeholder="商品数量" />
        </Form.Item>
      </Space>
    </Form.Item>
    

    <Form.Item label="商品id">
      <Space>
        <Form.Item
          name="shoppingid"
          noStyle
          rules={[{ required: true, message: '商品id' }]}
        >
          <Input style={{ width: 160 }} placeholder="商品id" />
        </Form.Item>
      </Space>
    </Form.Item>

    <Form.Item label="用户id">
      <Space>
        <Form.Item
          name="userid"
          noStyle
          rules={[{ required: true, message: '用户id' }]}
        >
          <Input style={{ width: 160 }} placeholder="用户id" />
        </Form.Item>
      </Space>
    </Form.Item>


    <Form.Item label=" " colon={false}>
      <Button type="primary" htmlType="submit">
       确认修改
      </Button>
    </Form.Item>
  </Form>
  }
